﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NModBusDrive
{
    public static class DOConvert
    {
        /// <summary>
        /// 转换为二进制
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ConvertToBinary(this ushort s)
        {
            return Convert.ToString(s, 2);
        }

        /// <summary>
        /// 转换为十进制
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static ushort ConvertToDecimal(this ushort[] s)
        {
            string str = string.Empty;
            for (var i=0;i<s.Length;i++)
            {
                if (i == 16)
                {
                    str += 0;
                }
                else
                {
                    str += s[i];
                }
            }
            return Convert.ToUInt16(str, 2);
        }

        /// <summary>
        /// 转换为16进制字符串
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ConvertToHex(this ushort[] s)
        {
            byte[] b = new byte[s.Length];
            for (var i = 0; i < s.Length; i++)
            {
                b[i] = (byte)s[i];
            }
            Array.Reverse(s);
            return BitConverter.ToString(b);
        }

        public static ushort ConvertDefautToDecimal(this ushort[] s)
        {
            string str = string.Empty;
            foreach (var item in s)
            {
                ushort _temp = 0;
                str += _temp;
            }
            return Convert.ToUInt16(str, 2);
        }

        /// <summary>
        /// 转换为string数据
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ConvertToString(this ushort[] s)
        {
            string str = string.Empty;
            foreach (var item in s)
            {
                str += item +"  ";
            }
            return str;
        }
    }
}
